package com.du.lin.utils;

import com.du.lin.bean.SalesManagement;
import com.du.lin.bean.User;
import net.sf.ehcache.transaction.xa.EhcacheXAException;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConventMessageUtil {


    public static List<Map<String,String>>  conventSalesManagement(List<SalesManagement> list,String date)throws Exception{
        List<Map<String,Object>> listMap = new ArrayList<>();
        //有多少天
        int days = DateUtil.getDaysByYearMonth(date);
        //重新组装数据
        list.forEach(element->{
            Map<String,String> map = new HashMap<>();
            //map.put("")

        });
        return null;
    }


    public static List<Object> RepeatMessage(List<SalesManagement> list, List<User> userList,String date)throws Exception{
        List<Map<String,Object>> idlList = new ArrayList<>();

        //返回给前端的数据
        List<Object> pageList = new ArrayList<>();
        //统计出有信息的用户
        List<Map<String,Object>> getMap = new ArrayList<>();
        //统计用户
        Map<String,Object> resultUserIdMap = new HashMap<String,Object>();
        //处理同一个用户信息
        for(int i=0;i<userList.size();i++){
            //统计唯一数据的
            Map<String,Object> resultMap = new HashMap<String,Object>();
            for (int j=0;j<list.size();j++){
                Map<String,Object> timeMap =new HashMap<>();
                    if(userList.get(i).getId().equals(list.get(j).getUserId())){
                        Object userIdObje = resultUserIdMap.get(list.get(j).getUserId().toString());
                        if(StringUtils.isEmpty(userIdObje) && StringUtils.isEmpty(resultMap.get("day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate()))))) {
                            //时间
                            timeMap.put("day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate())), list.get(j));
                            timeMap.put("list", list.get(j));
                            timeMap.put(list.get(i).getUserId().toString(), "day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate())));
                            //timeList.add(timeMap);
                            resultMap.put("day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate())), timeMap);
                            //userId
                            resultUserIdMap.put(list.get(j).getUserId().toString(),timeMap);
                            getMap.add(timeMap);
                        }else{
                            //Map<String,Map<String,Object>> map = (Map<String,Map<String, Object>>)resultMap.get(String.valueOf(list.get(j).getUserId()));
                            Object object  =  resultMap.get("day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate())));
                            if(StringUtils.isEmpty(object) && !StringUtils.isEmpty(userIdObje)){
                                timeMap.put("day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate())),list.get(j));
                                timeMap.put("list", list.get(j));
                                timeMap.put(list.get(i).getUserId().toString(), "day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate())));
                                resultMap.put("day"+String.valueOf(DateUtil.getDayByYearMonthDays(list.get(j).getCreateDate())),timeMap);
                                //userid
                                resultUserIdMap.put(list.get(j).getUserId().toString(),timeMap);
                                getMap.add(timeMap);
                            }
                        }

                    /*}*/
                }
            }
        }

        //要处理的
        List<Map<String,String>> typeList = new ArrayList<>();
        Map<String,String> dxs = new HashMap<>();
        dxs.put("typeName","电销数");
        dxs.put("isType","dxs");
        typeList.add(dxs);
        Map<String,String> jwxrs = new HashMap<>();
        jwxrs.put("typeName","加微信人数");
        jwxrs.put("isType","jwxrs");
        typeList.add(jwxrs);
        Map<String,String> lkhgs = new HashMap<>();
        lkhgs.put("typeName","老客户个数");
        lkhgs.put("isType","lkhgs");
        typeList.add(lkhgs);
        Map<String,String> xkhgs = new HashMap<>();
        xkhgs.put("typeName","新客户个数");
        xkhgs.put("isType","lkhgs");
        typeList.add(xkhgs);
        Map<String,String> lkhsqgs = new HashMap<>();
        lkhsqgs.put("typeName","老客户申请数");
        lkhsqgs.put("isType","lkhsqgs");
        typeList.add(lkhsqgs);
        Map<String,String> xkhsqgs = new HashMap<>();
        xkhsqgs.put("typeName","新客户申请数");
        xkhsqgs.put("isType","xkhsqgs");
        typeList.add(xkhsqgs);
        Map<String,String> lkhxkgs = new HashMap<>();
        lkhxkgs.put("typeName","老客户下款数");
        lkhxkgs.put("isType","lkhxkgs");
        typeList.add(lkhxkgs);
        Map<String,String> xkhxkgs = new HashMap<>();
        xkhxkgs.put("typeName","新客户下款数");
        xkhxkgs.put("isType","xkhxkgs");
        typeList.add(xkhxkgs);
        //处理数据
        userList.forEach(element->{
            //查看这个用户是否有值
            Map<String,Object> numberList = new HashMap<>();
            getMap.forEach(getMapElemen->{
                Map<String,Object> mapElemet = getMapElemen;
                if(!StringUtils.isEmpty(mapElemet.get(element.getId()+""))){
                    String  days= (String) mapElemet.get(String.valueOf(element.getId()));
                    numberList.put(days,mapElemet.get(days));
                }
            });
            for (int i=0;i<typeList.size();i++){

                switch (typeList.get(i).get("typeName")){
                    case "电销数":
                        Map<String,Object> map1 = new HashMap<>();
                        map1.put("userName",element.getUsername());
                        map1.put("type",typeList.get(i).get("typeName"));
                        map1.put("userId",element.getId());
                        map1.put("date",date);
                        map1.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map1,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map1);
                        break;
                    case "加微信人数":
                        Map<String,Object> map2 = new HashMap<>();
                        map2.put("userName",element.getUsername());
                        map2.put("type",typeList.get(i).get("typeName"));
                        map2.put("userId",element.getId());
                        map2.put("date",date);
                        map2.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map2,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map2);
                        break;
                    case "老客户个数":
                        Map<String,Object> map3 = new HashMap<>();
                        map3.put("userName",element.getUsername());
                        map3.put("type",typeList.get(i).get("typeName"));
                        map3.put("userId",element.getId());
                        map3.put("date",date);
                        map3.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map3,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map3);
                        break;
                    case "新客户个数":
                        Map<String,Object> map4 = new HashMap<>();
                        map4.put("userName",element.getUsername());
                        map4.put("type",typeList.get(i).get("typeName"));
                        map4.put("userId",element.getId());
                        map4.put("date",date);
                        map4.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map4,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map4);
                        break;
                    case "老客户申请数":
                        Map<String,Object> map5 = new HashMap<>();
                        map5.put("userName",element.getUsername());
                        map5.put("type",typeList.get(i).get("typeName"));
                        map5.put("userId",element.getId());
                        map5.put("date",date);
                        map5.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map5,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map5);
                        break;
                    case "新客户申请数":
                        Map<String,Object> map6 = new HashMap<>();
                        map6.put("userName",element.getUsername());
                        map6.put("type",typeList.get(i).get("typeName"));
                        map6.put("userId",element.getId());
                        map6.put("date",date);
                        map6.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map6,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map6);
                        break;
                    case "老客户下款数":
                        Map<String,Object> map7 = new HashMap<>();
                        map7.put("userName",element.getUsername());
                        map7.put("type",typeList.get(i).get("typeName"));
                        map7.put("userId",element.getId());
                        map7.put("date",date);
                        map7.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map7,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map7);
                        break;
                    case "新客户下款数":
                        Map<String,Object> map8 = new HashMap<>();
                        map8.put("userName",element.getUsername());
                        map8.put("type",typeList.get(i).get("typeName"));
                        map8.put("userId",element.getId());
                        map8.put("date",date);
                        map8.put("isType",typeList.get(i).get("isType"));
                        getMonthsDay(map8,numberList,date,typeList.get(i).get("typeName"));
                        pageList.add(map8);
                        break;
                }
            }
        });
        return pageList;
    }

    //根据当月的天数生产对于的数据

    /**
     *
     * @param map 需要设置的值
     * @param numberMap 查询出有数据的值
     * @param date  查询的那月的数据
     * @throws Exception
     */
    public static void  getMonthsDay(Map<String,Object> map,Map<String,Object> numberMap,String date,String type){
        //计算合计用的
        List<BigDecimal> sumBigDecimal = new ArrayList<>();
        //计算出当月的天数
        int monthDays = 0;
        try {
            monthDays = DateUtil.getDaysByYearMonth(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        for (int i=0;i<monthDays;i++){
            //当月的第几天
            int j = i+1;
            //如果有值就取出来
            if(!StringUtils.isEmpty(numberMap) && !StringUtils.isEmpty(numberMap.get("day"+j+""))){
                SalesManagement salesManagement = null;
                switch (type){
                    case "电销数":
                             salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                             if(StringUtils.isEmpty(salesManagement.getNumber())){
                                 map.put("day"+j,"0");
                             }else {
                                 map.put("day"+j,salesManagement.getNumber());
                             }
                             if(isRealNumber(salesManagement.getNumber())){
                                 sumBigDecimal.add(new BigDecimal(salesManagement.getNumber()));
                             }
                        break;
                    case "加微信人数":
                        salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                        if(StringUtils.isEmpty(salesManagement.getWxNumber())){
                            map.put("day"+j,"0");
                        }else {
                            map.put("day"+j,salesManagement.getWxNumber());
                        }
                        if(isRealNumber(salesManagement.getWxNumber())){
                            sumBigDecimal.add(new BigDecimal(salesManagement.getWxNumber()));
                        }
                        break;
                    case "老客户个数":
                        salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                        if(StringUtils.isEmpty(salesManagement.getOldCustomersNumber())){
                            map.put("day"+j,"0");
                        }else {
                            map.put("day"+j,salesManagement.getOldCustomersNumber());
                        }
                        if(isRealNumber(salesManagement.getOldCustomersNumber())){
                            sumBigDecimal.add(new BigDecimal(salesManagement.getOldCustomersNumber()));
                        }
                        break;
                    case "新客户个数":
                        salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                        if(StringUtils.isEmpty(salesManagement.getNewCustomersNumber())){
                            map.put("day"+j,"0");
                        }else {
                            map.put("day"+j,salesManagement.getNewCustomersNumber());
                        }
                        if(isRealNumber(salesManagement.getNewCustomersNumber())){
                            sumBigDecimal.add(new BigDecimal(salesManagement.getNewCustomersNumber()));
                        }
                        break;
                    case "老客户申请数":
                        salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                        if(StringUtils.isEmpty(salesManagement.getOldApplyNumber())){
                            map.put("day"+j,"0");
                        }else {
                            map.put("day"+j,salesManagement.getOldApplyNumber());
                        }
                        if(isRealNumber(salesManagement.getOldApplyNumber())){
                            sumBigDecimal.add(new BigDecimal(salesManagement.getOldApplyNumber()));
                        }
                        break;
                    case "新客户申请数":
                        salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                        if(StringUtils.isEmpty(salesManagement.getNewApplyNumber())){
                            map.put("day"+j,"0");
                        }else {
                            map.put("day"+j,salesManagement.getNewApplyNumber());
                        }
                        if(isRealNumber(salesManagement.getNewApplyNumber())){
                            sumBigDecimal.add(new BigDecimal(salesManagement.getNewApplyNumber()));
                        }
                        break;
                    case "老客户下款数":
                        salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                        if(StringUtils.isEmpty(salesManagement.getOldParagraphNumber())){
                            map.put("day"+j,"0");
                        }else {
                            map.put("day"+j,salesManagement.getOldParagraphNumber());
                        }
                        if(isRealNumber(salesManagement.getOldParagraphNumber())){
                            sumBigDecimal.add(new BigDecimal(salesManagement.getOldParagraphNumber()));
                        }
                        break;
                    case "新客户下款数":
                        salesManagement =(SalesManagement) numberMap.get("day"+j+"");
                        if(StringUtils.isEmpty(salesManagement.getNewParagraphNumber())){
                            map.put("day"+j,"0");
                        }else {
                            map.put("day"+j,salesManagement.getNewParagraphNumber());
                        }
                        if(isRealNumber(salesManagement.getNewParagraphNumber())){
                            sumBigDecimal.add(new BigDecimal(salesManagement.getNewParagraphNumber()));
                        }
                        break;
                }
            }else{
                map.put("day"+j,"0");
                sumBigDecimal.add(new BigDecimal(0));
            }
        }
        //计算总和
        final BigDecimal[] sum = {new BigDecimal(0)};
        sumBigDecimal.forEach(ele->{
            sum[0] = sum[0].add(ele);
        });
        map.put("sum", sum[0]);
    }


    /**
     * 判断是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    private static boolean isMatch(String regex, String orginal){
        if (orginal == null || orginal.trim().equals("")) {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher isNum = pattern.matcher(orginal);
        return isNum.matches();
    }

    public static boolean isPositiveInteger(String orginal) {
        return isMatch("^\\+{0,1}[1-9]\\d*", orginal);
    }

    public static boolean isNegativeInteger(String orginal) {
        return isMatch("^-[1-9]\\d*", orginal);
    }

    public static boolean isWholeNumber(String orginal) {
        return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);
    }

    public static boolean isPositiveDecimal(String orginal){
        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);
    }

    public static boolean isNegativeDecimal(String orginal){
        return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal);
    }

    public static boolean isDecimal(String orginal){
        return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);
    }

    public static boolean isRealNumber(String orginal){
        return isWholeNumber(orginal) || isDecimal(orginal);
    }


    public static void main(String arg[]){
        System.out.println(isRealNumber("25"));
    }
}
